package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.beautyescort.EscortCartData;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortMgr;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortRankMgr;
import com.yanqu.road.server.manager.beautyescort.CrossEscortPlayerWrap;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Set;

@Cmd(code = Protocol.C_BEAUTY_ESCORT_SEARCH_USER, desc = "查探玩家")
public class CrossBeautyEscortSearchCmd extends CrossCommand {

    long userServerId;
    long userAliasId;
    long playerId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.BeautyEscortSearchUserReqMsg reqMsg = BeautyEscortProto.BeautyEscortSearchUserReqMsg.parseFrom(packet.getBytes());
        userServerId = reqMsg.getServerId();
        userAliasId = reqMsg.getUserAliasId();
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        int groupId = CrossBeautyEscortRankMgr.getGroupId(serverId);
        Set<Long> serverIdSet = CrossBeautyEscortRankMgr.getGroupServer(groupId);
        if(!serverIdSet.contains(userServerId)){
            ret = GameErrorCode.E_BEAUTY_ESCORT_USER_NOT_IN_GROUP;
            return;
        }
        CrossEscortPlayerWrap playerWrap = CrossBeautyEscortMgr.searchUserByAliasId(userServerId, userAliasId);
        if(playerWrap == null){
            ret = GameErrorCode.E_BEAUTY_ESCORT_USER_NOT_FOUND;
            return;
        }
        playerWrap.calAllCartState();
        BeautyEscortProto.BeautyEscortSearchUserRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortSearchUserRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setTotalGlamour(playerWrap.getUserData().getTotalGlamour());
        respMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(playerWrap.getUserBaseInfo()));
        respMsg.setUserId(playerWrap.getUserData().getUserId());
        for (EscortCartData cartData : playerWrap.getCartDataMap().values()) {
            if(!playerWrap.canRob(cartData, ConfigMgr.getMaxRobTimesInLoss(), playerId)){
                continue;
            }
            //if(!playerWrap.isEscorting(cartData)){
            //    continue;
            //}
            BeautyEscortProto.BeautyEscortCartFindTemp.Builder cartBuilder = CrossBeautyEscortMgr.getSearchCartBuilder(cartData);
            //是否可劫镖
            cartBuilder.setCanRob(!cartData.getEnemy().contains(playerId));
            respMsg.addCart(cartBuilder);
        }
        MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_BEAUTY_ESCORT_SEARCH_USER_FROM_CROSS, respMsg));
    }



    @Override
    public void sendData() {
        if(ret != 0){
            BeautyEscortProto.BeautyEscortSearchUserRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortSearchUserRespMsg.newBuilder();
            respMsg.setRet(ret);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_BEAUTY_ESCORT_SEARCH_USER_FROM_CROSS, respMsg));
        }
    }
}
